using System;
using System.Drawing;
using Gtk;

namespace GtkControls
{
    
    [System.ComponentModel.Category("GtkControls")]
    [System.ComponentModel.ToolboxItem(true)]
    public partial class NetworkIndicatorWidget : Gtk.Bin
    {
        private double _signal;
        private string _networkName;
        private string _networkType;

        /// <summary>
        /// Get or set the signal value (0-100).
        /// </summary>
        public double Signal
        {
            get { return _signal*100; }
            set { _signal = value / 100; this.QueueDraw(); }
        }

        public string NetworkName {
            get { return _networkName; }
            set { _networkName = value; }
        }

        public string NetworkType {
            get { return _networkType; }
            set { _networkType = value; }
        }
        
        public NetworkIndicatorWidget()
        {
            this.Build();
        }

        protected virtual void OnDrawingareaExposeEvent (object o, Gtk.ExposeEventArgs args)
        {
            using(Cairo.Context ctx = Gdk.Graphics.CreateDrawable(this.drawingarea.GdkWindow))
            {
                 DrawBar(ctx, this.drawingarea.GdkWindow);
                 DrawText(ctx, this.drawingarea.GdkWindow);
            }
        }

        private void DrawBar(Cairo.Context ctx, Gdk.Window win)
        {
            int width, height;
            win.GetSize(out width, out height);
            
            int barWidth = (int)(width * _signal);
            
            ctx.Color = new Cairo.Color (0.0, 0.73, 0.0, 1.0);
            ctx.Rectangle(1, 1, barWidth, height);
            ctx.Fill();
            ctx.Color = new Cairo.Color (0.0, 0.0, 0.0);
            ctx.Rectangle(0, 0, width, height);
            ctx.Stroke();
        }
        
        private void DrawText(Cairo.Context ctx, Gdk.Window win)
        {
            string content = _networkName;
            if(!String.IsNullOrEmpty(_networkType))
                content += String.Format(" [{0}]", _networkType);
            
            ctx.Color = new Cairo.Color (0.0, 0.0, 0.0);
            ctx.MoveTo(7, 10);
            ctx.ShowText(content);
        }
    }
}
